<?php
	require_once('functions.php');
	
	// CATEGORY FUNCTIONS
	function GetCats(){
		$conn = GetConnection();
		return $results = $conn -> query('SELECT * FROM WSP2012_Category');
	}
	
	function GetCat($id){
		$conn= GetConnection();
		$sql = "SELECT * FROM WSP2012_Category WHERE category_id=$id ";
		$results = $conn -> query($sql);
		$row = $results -> fetch_assoc();
		$conn -> close();
		return $row;
	}
	
	function SaveCat($row){
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = "UPDATE `WSP2012_Category` " 
			.	"Set updated_at='$row2[updated_at]', category_name='$row2[category_name]', description='$row2[description]' "
			.	"WHERE category_id='$row2[id]' ";
		$results = $conn -> query($sql);
		$error = $conn -> error;
		$conn -> close();
		
		return $error == '' ? true : array('SQL Error' => $error);	
	}
	
	function CreateCat($row){
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = "INSERT INTO `WSP2012_Category` "
			.		"(category_id, created_at, updated_at, category_name, description) "
			.		"VALUES ('$row2[category_id]', '$row2[created_at]', '$row2[updated_at]', '$row2[category_name]', '$row2[description]') ";
		$results = $conn -> query($sql);
		$error = $conn -> error;
		$conn -> close();	
		
		return $error == '' ? true : array('SQL Error' => $error);
	}
	
	function DeleteCat($row, $id){
		$conn = GetConnection();
		$row2 = EscapeRow($row, $conn);
		$sql = "DELETE from WSP2012_Category WHERE category_id= '$row2[id]' ";
		$results = $conn -> query($sql);
		$error = $conn -> error;
		$conn -> close();	
		
		return $error == '' ? true : array('SQL Error' => $error);
	}
?>
